import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { getservicelist } from '@/api/service';
import { gethouselist, getsandlist } from '@/api/house';
import { getsecoundlist, gethirelist } from '@/api/hire';

import { useAddKey } from '@/hooks/useKey';

const IndexModel = {
  namespace: 'area',

  state: {
    name: '',
    list: [],
    searchlist: [],
    houseList: [],
    sandList: [],
    secoundList: [],
    hireList: [],
    searchSecoundList: [],
    searchHireList: [],
  },

  effects: {
    *getservicelist({ payload }, { call, put }) {
      const data = yield getservicelist();
      // console.log(data.data.data);
      yield put({ type: 'getlist', payload: data.data.data });
    },
    *gethouselist({ payload }, { call, put }) {
      const data = yield gethouselist();
      // console.log(data.data.data);
      yield put({ type: 'sethouselist', payload: data.data.data });
    },
    *getsandlist({ payload }, { call, put }) {
      const data = yield getsandlist();
      // console.log(data.data.data);
      yield put({ type: 'setsandlist', payload: data.data.data });
    },
    *getsecoundlist({ payload }, { call, put }) {
      const data = yield getsecoundlist();
      // console.log(data.data.data);
      yield put({ type: 'setsecoundlist', payload: data.data.data });
    },
    *gethirelist({ payload }, { call, put }) {
      const data = yield gethirelist();
      // console.log(data.data.data);
      yield put({ type: 'sethirelist', payload: data.data.data });
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    getlist(state, { payload }) {
      state.list = payload;
      useAddKey(state.list);
      state.searchlist = state.list;
      // console.log(state.list);
      return {
        ...state,
      };
    },
    delservice(state, { payload }) {
      // console.log(payload);
      state.list = state.list.filter((item) => item.kefu_id !== payload);
      return {
        ...state,
      };
    },
    addservice(state, { payload }) {
      payload.kefu_id = state.list[state.list.length - 1].kefu_id + 1;
      payload.key = state.list[state.list.length - 1].kefu_id + 1;
      let list = [...state.list];
      console.log(payload);
      list.push(payload);
      return {
        ...state,
        list: list,
      };
    },
    editname(state, { payload }) {
      // console.log(payload);
      const { id, kefu_name } = payload;
      let list = JSON.parse(JSON.stringify(state.list));
      let ind = list.findIndex((item) => item.kefu_id === id);
      console.log(ind);
      list[ind].kefu_name = kefu_name;
      return {
        ...state,
        list: list,
      };
    },
    searchservice(state, { payload }) {
      let list = JSON.parse(JSON.stringify(state.searchlist));
      list = list.filter((item) => {
        return item.kefu_name.includes(payload);
      });
      return {
        ...state,
        list: list,
      };
    },
    sethouselist(state, { payload }) {
      // let houses=JSON.parse(JSON.stringify(state.houseList))
      // houses = [...payload];
      // useAddKey(state.list)
      // console.log(state.list);
      return {
        ...state,
        houseList: payload,
      };
    },
    edithouselist(state, { payload }) {
      const { id, values } = payload;
      let sands = JSON.parse(JSON.stringify(state.houseList));
      let ind = sands.findIndex((item) => item.id === id);
      sands[ind] = { ...sands[ind], ...values };
      return {
        ...state,
        houseList: sands,
      };
    },
    addhouselist(state, { payload }) {
      let sands = JSON.parse(JSON.stringify(state.houseList));
      payload.id = state.houseList[state.houseList.length - 1].id + 1;
      payload.key = payload.id;
      sands.push({ ...state.houseList[state.houseList.length - 1],...payload });
      // console.log(sands);
      return {
        ...state,
        houseList: sands,
      };
    },
    delhouse(state, { payload }) {
      let houselist = JSON.parse(JSON.stringify(state.houseList));
      let ind = houselist.findIndex((item) => item.id === payload);
      houselist.splice(ind, 1);
      return {
        ...state,
        houseList: houselist,
      };
    },

    setsandlist(state, { payload }) {
      let list = useAddKey(payload);
      return {
        ...state,
        sandList: list,
      };
    },
    delsand(state, { payload }) {
      let sands = JSON.parse(JSON.stringify(state.sandList));
      let ind = sands.findIndex((item) => item.id === payload);
      sands.splice(ind, 1);
      return {
        ...state,
        sandList: sands,
      };
    },
    addsand(state, { payload }) {
      payload.id = state.sandList[state.sandList.length - 1].id + 1;
      payload.key = payload.id;
      let sands = JSON.parse(JSON.stringify(state.sandList));
      sands.push(payload);
      // console.log(sands);
      return {
        ...state,
        sandList: sands,
      };
    },
    editsand(state, { payload }) {
      const { id } = payload;
      let sands = JSON.parse(JSON.stringify(state.sandList));
      let ind = sands.findIndex((item) => item.id === id);
      sands[ind] = { ...sands[ind], ...payload };
      return {
        ...state,
        sandList: sands,
      };
    },

    setsecoundlist(state, { payload }) {
      let list = useAddKey(payload);
      // console.log(list);
      return {
        ...state,
        secoundList: list,
        searchSecoundList: list,
      };
    },
    editsecoundlist(state, { payload }) {
      const { id, values } = payload;
      let sands = JSON.parse(JSON.stringify(state.secoundList));
      let ind = sands.findIndex((item) => item.id === id);
      sands[ind] = { ...sands[ind], ...values };
      return {
        ...state,
        secoundList: sands,
      };
    },
    addsecoundlist(state, { payload }) {
      let sands = JSON.parse(JSON.stringify(state.secoundList));
      payload.id = state.secoundList[state.secoundList.length - 1].id + 1;
      payload.key = payload.id;
      sands.push({ ...state.secoundList[state.secoundList.length - 1],...payload });
      // console.log(sands);
      return {
        ...state,
        secoundList: sands,
      };
    },
    sethirelist(state, { payload }) {
      let list = useAddKey(payload);
      // console.log(list);
      return {
        ...state,
        hireList: list,
        searchHireList: list,
      };
    },
    edithirelist(state, { payload }) {
      const { id, values } = payload;
      let sands = JSON.parse(JSON.stringify(state.hireList));
      let ind = sands.findIndex((item) => item.id === id);
      sands[ind] = { ...sands[ind], ...values };
      return {
        ...state,
        hireList: sands,
      };
    },
    addhirelist(state, { payload }) {
      let sands = JSON.parse(JSON.stringify(state.hireList));
      payload.id = state.hireList[state.hireList.length - 1].id + 1;
      payload.key = payload.id;
      sands.push({ ...state.hireList[state.hireList.length - 1],...payload });
      // console.log(sands);
      return {
        ...state,
        hireList: sands,
      };
    },
    delsecound(state, { payload }) {
      let sands = JSON.parse(JSON.stringify(state.secoundList));
      let ind = sands.findIndex((item) => item.id === payload);
      sands.splice(ind, 1);
      return {
        ...state,
        secoundList: sands,
      };
    },
    delhire(state, { payload }) {
      let sands = JSON.parse(JSON.stringify(state.hireList));
      let ind = sands.findIndex((item) => item.id === payload);
      sands.splice(ind, 1);
      return {
        ...state,
        hireList: sands,
      };
    },
    searchsecound(state, { payload }) {
      let list = JSON.parse(JSON.stringify(state.searchSecoundList));
      list = list.filter((item) => {
        return item.estate_name.includes(payload);
      });
      return {
        ...state,
        secoundList: list,
      };
    },
    searchhire(state, { payload }) {
      let list = JSON.parse(JSON.stringify(state.searchHireList));
      list = list.filter((item) => {
        return item.estate_name.includes(payload);
      });
      return {
        ...state,
        hireList: list,
      };
    },
    // 启用 immer 之后 state为proxy
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    // setup({ dispatch, history }) {
    //   console.log('setup subscription');
    //   return history.listen(({ pathname }) => {
    //     if (pathname !== '/login' && !sessionStorage.getItem('token')) {
    //       history.push('/login');
    //     }
    //   });
    // },
  },
};

export default IndexModel;
